//
//  LBEffect6Filter.mm
//  OpenGLGame
//
//  Created by Nguyen Cong Huy on 11/22/12.
//  Copyright (c) 2012 UIT. All rights reserved.
//

#include <iostream>
#include "LBEffect6Filter.h"

char fShaderStrEffect6[] = ""
"precision highp float;  "
"varying highp vec2 vtexCoor; "
"uniform sampler2D texture; "

"float safeColorF(float color){"
"    if(color > 1.0) color = 1.0;"
"    else if(color < 0.0) color = 0.0;"
"    return color;"
"}"

"vec4 safeColor(vec4 color){"
"    color.r = safeColorF(color.r);"
"    color.g = safeColorF(color.g);"
"    color.b = safeColorF(color.b);"
"    return color;"
"}"

"void main() "
"{ "
"     vec4 textureColor = texture2D(texture, vtexCoor); "

//statuation
"highp float statuation = 0.4; "
"float avg = (textureColor.r + textureColor.g + textureColor.b)/3.0; "
"textureColor.r = avg + statuation*(textureColor.r - avg); "
"textureColor.g = avg + statuation*(textureColor.g - avg);"
"textureColor.b = avg + statuation*(textureColor.b - avg);"

"textureColor = safeColor(textureColor);"

//contrast
"float contrast = 0.75;"
"textureColor.r = (textureColor.r - 0.5)*contrast + 0.5;"
"textureColor.g = (textureColor.g - 0.5)*contrast + 0.5;"
"textureColor.b = (textureColor.b - 0.5)*contrast + 0.5;"

"textureColor = safeColor(textureColor);"

//tin effect
"vec4 maxColor = vec4(0.588, 0.627, 0.902, 1.0);"
"vec4 minColor = vec4(0.078, 0.137, 0.039, 1.0);"
"textureColor.r = (textureColor.r - minColor.r)*(1.0/(maxColor.r - minColor.r));"
"textureColor.g = (textureColor.g - minColor.g)*(1.0/(maxColor.g - minColor.g));"
"textureColor.b = (textureColor.b - minColor.b)*(1.0/(maxColor.b - minColor.b));"

"textureColor = safeColor(textureColor);"


"    gl_FragColor = textureColor; "
"} ";

LBEffect6Filter::LBEffect6Filter(){
    init(0, fShaderStrEffect6);
}